package com.miyake.dsp.generator.ui;

import com.miyake.dsp.generator.PulseSetting;
import com.miyake.dsp.ui.DspTextField;
import com.miyake.dsp.ui.DspWidget;

public class PulseSettingPanel extends DspWidget {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7320760791349288008L;
	private PulseSetting setting;

	public PulseSettingPanel(PulseSetting setting) {
		this.setName("Pulse Setting");
		this.setting = setting;
		createWidth(setting.getWidth());
		createMagnitude(setting.getMagnitude());
		createMargin(setting.getMargin());
	}

	private void createMargin(double margin) {
		DspTextField text = new DspTextField("Margin[%]");
		text.setValue(String.valueOf(margin));
		this.add(text);
		
		text.addHandler(new DspWidgetHandler() {
			@Override
			public void onChange(String value) {
				setting.setMargin(Double.valueOf(value));
				fireChange(value);
			}
		});	
	}

	private void createWidth(double width) {
		DspTextField text = new DspTextField("Width[s]");
		text.setValue(String.valueOf(width));
		this.add(text);
		
		text.addHandler(new DspWidgetHandler() {
			@Override
			public void onChange(String value) {
				setting.setWidth(Double.valueOf(value));
				fireChange(value);
			}
		});	
	}
	
	private void createMagnitude(double mag) {
		DspTextField text = new DspTextField("Magnitude");
		text.setValue(String.valueOf(mag));
		this.add(text);
		
		text.addHandler(new DspWidgetHandler() {
			@Override
			public void onChange(String value) {
				setting.setMagnitude(Double.valueOf(value));
				fireChange(value);
			}
		});	
	}
}
